clear

log using governor_details.log

use "governor_details.dta"

*set up fe
egen industry_name_CSRC_f=group(industry_name_CSRC)
egen industry_year=group(industry_name_CSRC year)
egen province_year=group(provid year)
encode current_gvn, gen(current_gvn_f)

*sanity check
unique current_gvn //75 

******************
*PROMOTION
******************
*Figure 2 - Analysis on provincial governor subgroups: Promoted v. not
preserve
keep if gvn_promote2==1
forval x=1/5{
quietly reghdfe subsidy_assets_w  i.L`x'.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
estimates store gvn_lag`x' //store the estimates of the entire regression and name it
}

coefplot gvn_lag1 gvn_lag2 gvn_lag3 gvn_lag4 gvn_lag5, /// 
    keep(1L.gvn_turnover#1.private 1L2.gvn_turnover#1.private ///
	1L3.gvn_turnover#1.private 1L4.gvn_turnover#1.private 1L5.gvn_turnover#1.private) ///
     yline(0, lp(dash))  vertical ///   
	coeflabels(1L.gvn_turnover#1.private="+1" ///
	1L2.gvn_turnover#1.private="+2" 1L3.gvn_turnover#1.private="+3" ///
	1L4.gvn_turnover#1.private="+4" 1L5.gvn_turnover#1.private="+5") /// 
	xtitle("Years after governor turnover", size(small)) ///
	levels(95 90) legend(order(1 "95%" 2 "90%") rows(1) position(6))  ciopts(lwidth(*1 *2) lcolor(gray black)) msymbol(C) ///
	scheme(lean1)  

graph export Figure2a.png, replace 
restore

preserve
keep if gvn_promote2==0
forval x=1/5{
quietly reghdfe subsidy_assets_w  i.L`x'.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
estimates store gvn_lag`x'  
}

coefplot gvn_lag1 gvn_lag2 gvn_lag3 gvn_lag4 gvn_lag5, ///code to generate the coefficient plot
    keep(1L.gvn_turnover#1.private 1L2.gvn_turnover#1.private ///
	1L3.gvn_turnover#1.private 1L4.gvn_turnover#1.private 1L5.gvn_turnover#1.private) ///matrix list e(b): recall coefficients saved previously but only use coefficients of interest
     yline(0, lp(dash))  vertical /// line through 0 on the x-axis and hide legend, vertical plot 
	coeflabels(1L.gvn_turnover#1.private="+1" ///
	1L2.gvn_turnover#1.private="+2" 1L3.gvn_turnover#1.private="+3" ///
	1L4.gvn_turnover#1.private="+4" 1L5.gvn_turnover#1.private="+5") ///rename coefficients 
	xtitle("Years after governor turnover", size(small)) ///
	levels(95 90) legend(order(1 "95%" 2 "90%") rows(1) position(6))  ciopts(lwidth(*1 *2) lcolor(gray black)) msymbol(C) ///
	scheme(lean1)  
	
graph export Figure2b.png, replace 
restore

*Table A10 - Promotion of governors by tenure length
preserve
keep province year current_gvn gvn_tenure gvn_promote
duplicates drop
tabout gvn_tenure gvn_promote using TableA10.tex, style(tex) format(0) replace
restore

/*
*calculation of % promoted
[0/(7+0)]*100=0
[2/(25+2)]*100=7.4 
[2/(48+2)]*100=4
[3/(34+3)]*100=8.1
[7/(28+7)]*100=20
[24/(8+24)]*100=75
[16/(23+16)]*100=41
[0/(8+0)]*100=0
[8/(5+8)]*100=61.5
*/

********************
*RETIREMENT
********************
*keep only governors that leave the post close to retirement age 65 (63,64,65) 
gen gvn_age_num=real(gvn_age)
drop if gvn_age=="NA"

*calculate governor starting age
egen gvn_age_max = max(gvn_age_num), by(current_gvn province)
gen gvn_age_start=gvn_age_max-gvn_tenure 
egen gvn_start=min(gvn_age_start>58), by(current_gvn province)
tab gvn_start
keep if gvn_start

*Table A13 - Governors near retirement 
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private, absorb(year industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass1
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_revenue_w, absorb(year industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass2
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w, absorb(year industry_name_CSRC_f) vce(cluster provid)
est sto gvn_lag1_subass3
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(industry_year) vce(cluster provid)
est sto gvn_lag1_subass4
reghdfe subsidy_assets_w i.L.gvn_turnover##i.private L.log_assets_w L.ROA_w, absorb(current_gvn_f industry_year) vce(cluster provid)
est sto gvn_lag1_subass6

outreg2 [gvn_lag1_subass1 gvn_lag1_subass2 gvn_lag1_subass3 gvn_lag1_subass4 gvn_lag1_subass6] using TableA13.tex, replace label dec(3)

log close
